#############################################/
## R-Script Master - If it ain't broke      ##
## by: Christian Schimpf et al.             ##
## -- Replication Material (Recoding)       ##
## Version: Dec. 16, 2020                   ##
## Last updated: August 25, 2021            ##
## R-Version: 4.1.0.                        ##
#############################################/


#read in dataset
df_Alberta <- read.csv("C:/Users/User/Dropbox (Personal)/Research Projects_Post Doc Ideas/Environmental Politics COpy/Confidence in oil and gas business/Analysis/Alberta_Survey_Final_20190612.csv")

###################################################################/
#### /// Section 1: Data preparation                           #####
##################################################################/

#### >>> Prep policy variables

#### >>> Reverse so that higher means more support
df_Alberta$Q222_1 <- car::recode(df_Alberta$Q222_1, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_2 <- car::recode(df_Alberta$Q222_2, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_3 <- car::recode(df_Alberta$Q222_3, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_4 <- car::recode(df_Alberta$Q222_4, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_5 <- car::recode(df_Alberta$Q222_5, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_6 <- car::recode(df_Alberta$Q222_6, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_7 <- car::recode(df_Alberta$Q222_7, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_8 <- car::recode(df_Alberta$Q222_8, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_9 <- car::recode(df_Alberta$Q222_9, "4=1; 3=2; 2=3; 1=4")
df_Alberta$Q222_10 <- car::recode(df_Alberta$Q222_10, "4=1; 3=2; 2=3; 1=4")

#### >>> Turn policy variables into factors (for descriptive stats)
df_Alberta$CR <- factor(df_Alberta$Q222_1, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$CT <- factor(df_Alberta$Q222_2, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$TIEE <- factor(df_Alberta$Q222_3, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$MFEE <- factor(df_Alberta$Q222_4, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
# Q222_5 not included in analyses
#df_Alberta$Q222_5 <- factor(df_Alberta$Q222_5, levels= c("1", "2",
#                                                         "3", "4"), ordered = T)
df_Alberta$MT <- factor(df_Alberta$Q222_6, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$SSR <- factor(df_Alberta$Q222_7, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$EOSC <- factor(df_Alberta$Q222_8, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$BMP <- factor(df_Alberta$Q222_9, levels= c("1", "2",
                                                         "3", "4"), ordered = T)
df_Alberta$CE <- factor(df_Alberta$Q222_10, levels= c("1", "2",
                                                          "3", "4"), ordered = T)

levels(df_Alberta$CR) <- c("Strongly oppose", "Oppose",
                                      "Support", "Strongly support")
levels(df_Alberta$CT) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$TIEE) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$MFEE) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$MT) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$SSR) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$EOSC) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$BMP) <- c("Strongly oppose", "Oppose",
                               "Support", "Strongly support")
levels(df_Alberta$CE) <- c("Strongly oppose", "Oppose",
                                "Support", "Strongly support")



#### >>> Recode oil and gas variable ####
df_Alberta$Q228[df_Alberta$Q228==2] <- 0
df_Alberta$Q228 <- as.factor(df_Alberta$Q228)
levels(df_Alberta$Q228) <- c("Not in oil and gas sector", "In oil and gas sector")
df_Alberta$OandG <- df_Alberta$Q228

#### >>> Recode Party ID Variable ####

# Coding:
# 0= Respondent does not identify with NDP but identifies with other party
# 1= Respondent identifies with NDP
# DK are coded to NA

df_Alberta$PID_NDP <- NA
df_Alberta$PID_NDP <- ifelse(df_Alberta$Q226==3,1,0)
df_Alberta$PID_NDP[df_Alberta$Q226==9] <- NA #DKs
df_Alberta$PID_NDP <- as.factor(df_Alberta$PID_NDP)
levels(df_Alberta$PID_NDP) <- c("Non NDP", "NDP")
table(df_Alberta$PID_NDP)


#### Belief in climate change
## Dichotomy with 0=R does not belief that climate change 
## is happening and 1=R beliefs that climate is happening
df_Alberta$ClimateChBelief <- NA 
df_Alberta$ClimateChBelief[df_Alberta$Q12==2] <- 0
df_Alberta$ClimateChBelief[df_Alberta$Q12==1] <- 1
table(df_Alberta$ClimateChBelief, df_Alberta$Q12, useNA = "always")

#### >>> Left-Right (Self Placement)
# 0=very left
# .
# .
# 10=very right
df_Alberta$LR <- df_Alberta$Q15
df_Alberta$LR_01 <- df_Alberta$LR/10

# LR self-placement by PID:
df_Alberta %>%
  group_by(Q226) %>%
  summarize(mean = mean(LR, na.rm=T))

#### >>> Importance of oil and gas for AB industry in 25 years ####
# " OandG will be ABs most important industry in 25 years"
# 4=Strongly agree
# 3=Agree
# 2=Disagree
# 1=Strongly disagree
df_Alberta$OandG_Fut_Importance <- df_Alberta$Q10_13 
df_Alberta$OandG_Fut_Importance <- car::recode(df_Alberta$OandG_Fut_Importance, "4=1; 3=2; 2=3; 1=4")
#df_Alberta$OandG_Fut_Importance <- as.factor(df_Alberta$OandG_Fut_Importance)
#levels(df_Alberta$OandG_Fut_Importance) <- c("Strongly disagree", "Disagree", "Agree", "Strongly agree")
table(df_Alberta$OandG_Fut_Importance)

#### >>> AB economy too depend on oil and gas
# "Alberta's economy is too dependent on oil and gas."
# 4=Strongly agree
# 3=Agree
# 2=Disagree
# 1=Strongly disagree
df_Alberta$AB_Dependent_OandG <- df_Alberta$Q10_12 
df_Alberta$AB_Dependent_OandG <- car::recode(df_Alberta$AB_Dependent_OandG,  "4=1; 3=2; 2=3; 1=4")
#df_Alberta$AB_Dependent_OandG <- as.factor(df_Alberta$AB_Dependent_OandG)
#levels(df_Alberta$AB_Dependent_OandG) <- c("Strongly disagree", "Disagree", "Agree", "Strongly agree")


table(df_Alberta$AB_Dependent_OandG)

#### >>> Proud of Alberta oil and gas industry ####
# Statement: I am proud of AB Oil and Gas industry
## 1=Strongly disagree
## 2=Disagree
## 3=Agree
## 4=Strongly agree
df_Alberta$OG_Pride <- df_Alberta$statement6
df_Alberta$OG_Pride <- car::recode(df_Alberta$OG_Pride,  "4=1; 3=2; 2=3; 1=4")
table(df_Alberta$OG_Pride)


#### >>> Demographics & Income  ####

## Age
df_Alberta$Age <- df_Alberta$Q234

## Age Group
df_Alberta$AgeCat <- NA
df_Alberta$AgeCat[df_Alberta$Age<25] <- 1
df_Alberta$AgeCat[df_Alberta$Age>24 & df_Alberta$Age<35] <- 2
df_Alberta$AgeCat[df_Alberta$Age>34 & df_Alberta$Age<45] <- 3
df_Alberta$AgeCat[df_Alberta$Age>44 & df_Alberta$Age<55] <- 4
df_Alberta$AgeCat[df_Alberta$Age>54 & df_Alberta$Age<55] <- 5
df_Alberta$AgeCat[df_Alberta$Age>54 & df_Alberta$Age<65] <- 6
df_Alberta$AgeCat[df_Alberta$Age>64 ] <- 7

## Gender
# 0 = female
# 1 = male
df_Alberta$Gender <- NA
df_Alberta$Gender[df_Alberta$Q235==1] <- 1
df_Alberta$Gender[df_Alberta$Q235==2] <- 0


## Education (based on attainment)
# 0=No completed university degree
# 1=University degree 
df_Alberta$Edu <- NA
df_Alberta$Edu <- ifelse(df_Alberta$Q238<9,0,df_Alberta$Edu)
df_Alberta$Edu <- ifelse(df_Alberta$Q238>8 &
                           df_Alberta$Q238<12 ,1,df_Alberta$Edu)
table(df_Alberta$Q238 ,df_Alberta$Edu)
df_Alberta$Edu <- as.factor(df_Alberta$Edu)
levels(df_Alberta$Edu) <- c("No University Education", "University Education")

## Rural/Suburban/Urban
# 1=Urban
# 2=Suburban
# 3=Rural
df_Alberta$Urban <- as.factor(df_Alberta$Q233)
levels(df_Alberta$Urban) <- c("Urban", "Suburban", "Rural")

## Income 
# 1= less than 20,000 CAN $
# ...
# 18= 1 Mio or more CAN $ 
df_Alberta$Income <- as.numeric(df_Alberta$Q242)

### Save Dataset as csv.

#Note: we do not include the original response ID but instead,
# a running ID. 
df_Alberta$ID <- seq.int(nrow(df_Alberta))

# generate subset of relevant variables and save
vars_sub <- c("ID", "Q222_1", "Q222_2", "Q222_3", "Q222_4", "Q222_6", "Q222_7", "Q222_8", "Q222_9",
              "OandG_Fut_Importance", "AB_Dependent_OandG",
              "OandG", "OG_Pride", "ClimateChBelief", "LR", "PID_NDP", "Gender", 
              "Edu", "Age", "Urban", "Income")

df_Alberta <- df_Alberta[vars_sub]
write.csv(df_Alberta,'Data_Schimpfetal_Replication_25082021.csv')

################# /// END OF R-Script #########################/
